﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthGetAction("ETL", "GetDataTableDataList", "1.0")]
    public class GetDataTableDataListAction : BaseAction<GetDataTableDataListParams, GetDataTableDataListResult>
    {
        protected override ActionResult<GetDataTableDataListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDataTableDataListParams opParams)
        {
            SER.DtTableService service = new SER.DtTableService();
            int total = 0;
            ServiceResult<MOD.MDtTableData> serResult = service.GetData(opParams.TableId, opParams.Page * 25, 25, out total);
            if (serResult.IsSuccess)
            {
                GetDataTableDataListResult actResult = new GetDataTableDataListResult();
                actResult.Page = opParams.Page;
                actResult.Start = serResult.Data.Start;
                actResult.Limit = serResult.Data.Limit;
                actResult.Total = serResult.Data.Total;
          
                actResult.Count = serResult.Data.DataTable.Rows.Count;

                for (int pkIndex = 0; pkIndex < serResult.Data.PKCloumns.Count; pkIndex++)
                {
                    actResult.PKColumns.Add(serResult.Data.PKCloumns[pkIndex]);
                }

                List<string> columns = new List<string>();
                List<string> types = new List<string>();
                for (int colIndex = 0; colIndex < serResult.Data.DataTable.Columns.Count; colIndex++)
                {
                    columns.Add(serResult.Data.DataTable.Columns[colIndex].ColumnName);
                    types.Add(serResult.Data.DataTable.Columns[colIndex].DataType.FullName);
                }
                actResult.Columns = columns;
                actResult.Types = types;

                for (int rowIndex = 0; rowIndex < serResult.Data.DataTable.Rows.Count; rowIndex++)
                {
                    System.Data.DataRow dataRow = serResult.Data.DataTable.Rows[rowIndex];
                    GetDataTableDataListResult.Item item = new GetDataTableDataListResult.Item();

                    for (int colIndex = 0; colIndex < actResult.Columns.Count; colIndex++)
                    {
                        item.Add(Convert.ToString(dataRow[actResult.Columns[colIndex]]));
                    }

                    actResult.Items.Add(item);
                }

                return new ActionResult<GetDataTableDataListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetDataTableDataListResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataTableDataListParams
    {
        [Parameter]
        public Guid TableId { get; set; }

        [Parameter]
        public int Page { get; set; }
    }

    public class GetDataTableDataListResult : PagedResult
    {
        public List<string> Columns { get; set; }
        /// <summary>
        /// 主键列，主键列是不允许修改的，修改数据的时候需要主键列的信息
        /// </summary>
        public List<string> PKColumns = new List<string>();
        public List<string> Types { get; set; }
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public List<string> Values { get; set; }

            public Item()
            {
                Values = new List<string>();
            }

            public void Add(string value)
            {
                Values.Add(value);
            }
        }
    }
}
